Add webtatic to yum repo:
  cmd.run:
    - name: rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
    - unless: test -s /etc/yum.repos.d/webtatic.repo

Apply sysctl kernel.shmmax for Apcu issue:
  sysctl.present:
    - name: kernel.shmmax
    - value: 68719476736

Enable webtatic-archive repo for older PHP version:
# Bug in 2015.8 This is not working.
# Resort into yum-config-manager for now
#  pkgrepo.managed:
#    - name: webtatic-archive
#    - enabled: True
  cmd.run:
    - name: yum-config-manager --enable webtatic-archive

{% set php_version = salt['pillar.get']('php:version') %}
{% set php_pkg_prefix = "php" + php_version[:1] + php_version[2:3] + "w" %}

Setup php:
  pkg.installed:
    # This is required to install older version of PHP
    # - fromrepo: webtatic-archive
    - pkgs:
      - {{ php_pkg_prefix }}: {{ php_version }}
    {% for pkg, enable_status in salt['pillar.get']('php:packages').items() %}
    {% if enable_status %}
      - {{ php_pkg_prefix }}-{{ pkg }}: {{ php_version }}
    {% endif %}
    {% endfor %}

# PECL packages are not depend on php version, but it's own version instead
Setup pecl packages:
  pkg.installed:
    - pkgs:
    {% for pkg, pecl_version in salt['pillar.get']('php:pecl_packages').items() %}
    {% if pecl_version == True %}
      - {{ php_pkg_prefix }}-pecl-{{ pkg }}
    {% elif pecl_version %}
      - {{ php_pkg_prefix }}-pecl-{{ pkg }}: {{ pecl_version }}
    {% endif %}
    {% endfor %}

Copy php.ini config file:
  file.managed:
    - name: /etc/php.ini
    - source: salt://php/files/php.ini
    - user: root
    - group: root
    - mode: 644
    - template: jinja

Copy freetds.conf config file:
  file.managed:
    - name: /etc/freetds.conf
    - source: salt://php/files/freetds.conf
    - user: root
    - group: root
    - mode: 644

{% if salt['pillar.get']('php:pecl_packages:apcu') %}
# Inculde APC Config only if set in pillar
Copy apcu.ini config file:
  file.managed:
    - name: /etc/php.d/apcu.ini
    - source: salt://php/files/apcu.ini
    - user: root
    - group: root
    - mode: 644

{% else %}
# And remove the file if not set in pillar
Remove apcu.ini config file:
  file.absent:
    - name: /etc/php.d/apcu.ini
{% endif %}

Create php5 symlink:
  file.symlink:
    - name: /usr/bin/php5
    - target: /usr/bin/php
